﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WC3Game;
using System.Collections.ObjectModel;
using Visifire.Commons;
using Visifire.Charts;


namespace DotaStats.Pages
{
    /// <summary>
    /// Interaction logic for HeroKills.xaml
    /// </summary>
    public partial class PlayerPerformance : Page
    {
        Game selectedGame = null;
        Player selectedPlayer = null;
        Team selectedTeam = null;


        Chart chart = null;
        List<DataSeries> deathsSeries = new List<DataSeries>();
        List<DataSeries> killsSeries = new List<DataSeries>();
        DataSeries quitSeries = new DataSeries();

        public PlayerPerformance(Game game, Player player, Team team)
        {
            InitializeComponent();

            selectedGame = game;
            selectedPlayer = player;
            selectedTeam = team;

            /*
            ObservableCollection<Kill> kills = new ObservableCollection<Kill>(player.kills);

            victimNameColumn.DisplayMemberBinding = new Binding("VictimName");
            timeColumn.DisplayMemberBinding = new Binding("Time");
            killTypeColumn.DisplayMemberBinding = new Binding("Type");
            listView1.ItemsSource = kills;
            */


            if (player != null)
                CreatePersonalChart();
            else
                CreateTeamChart();
        }

        public void CreateTeamChart()
        {
            chart = new Chart();
            chart.ScrollingEnabled = false;

            Axis xAxis = new Axis();
            xAxis.IntervalType = IntervalTypes.Minutes;

            chart.AxesX.Add(xAxis);

            DataPoint dataPoint = null;

            DataSeries quitSeries = new DataSeries();
            quitSeries.LegendText = "Leavers";
            quitSeries.RenderAs = RenderAs.Point;

            foreach (Player p in selectedGame.PlayerList)
            {
                if (p != null && p.Quit)
                {
                    dataPoint = new DataPoint();
                    TimeSpan leaveTime = p.QuitAt;
                    float leaveTimePercent = selectedGame.GetTimePercent(p.QuitAtString);
                    dataPoint.XValue = (int)leaveTimePercent;
                    dataPoint.YValue = 1;
                    dataPoint.AxisXLabel = String.Format("{1} left at {0}", leaveTime.ToString(), p.Name);
                    quitSeries.DataPoints.Add(dataPoint);
                }
            }
            chart.Series.Add(quitSeries);



            foreach (Player teamPlayer in selectedTeam.TeamPlayers)
            {
                DataSeries killSeries = new DataSeries();
                killSeries.LegendText = teamPlayer.Name + " Kills";

                DataSeries deathSeries = new DataSeries();
                deathSeries.LegendText = teamPlayer.Name + " Deaths";


                killSeries.RenderAs = RenderAs.Line;
                deathSeries.RenderAs = RenderAs.Line;

                dataPoint = new DataPoint();
                dataPoint.YValue = 0;

                killSeries.DataPoints.Add(dataPoint);
                deathSeries.DataPoints.Add(dataPoint);

                int killCount = 1;
                int deathCount = 1;

                foreach (Kill kill in teamPlayer.kills)
                {
                    if (kill.Type != Kill.KillType.Hero)
                        continue;

                    dataPoint = new DataPoint();
                    dataPoint.YValue = killCount;

                    float killTimePercent = selectedGame.GetTimePercent(kill.TimeString);

                    TimeSpan killTime = kill.Time;

                    dataPoint.XValue = (int)killTimePercent;
                    dataPoint.AxisXLabel = String.Format("{2} killed {0} at {1}", kill.VictimName, killTime.ToString(), teamPlayer.Name);

                    killSeries.DataPoints.Add(dataPoint);
                    killCount++;
                }

                foreach (Kill death in teamPlayer.deaths)
                {
                    dataPoint = new DataPoint();
                    dataPoint.YValue = deathCount;

                    float deathTimePercent = selectedGame.GetTimePercent(death.TimeString);

                    TimeSpan deathTime = death.Time;

                    dataPoint.XValue = (int)deathTimePercent;
                    dataPoint.AxisXLabel = String.Format("{2} killed by {0} at {1}", death.KillerName, deathTime.ToString(), teamPlayer.Name);

                    deathSeries.DataPoints.Add(dataPoint);
                    deathCount++;
                }

                killsSeries.Add(killSeries);
                deathsSeries.Add(deathSeries);

                chart.Series.Add(killSeries);
                chart.Series.Add(deathSeries);
            }
            heroKillGrid.Children.Add(chart);
        }

        public void CreatePersonalChart()
        {
          chart = new Chart();
            chart.ScrollingEnabled = false;

            Axis xAxis = new Axis();
            xAxis.IntervalType = IntervalTypes.Minutes;

            chart.AxesX.Add(xAxis);

            DataSeries killSeries = new DataSeries();
            DataSeries deathSeries = new DataSeries();
            DataSeries quitSeries = new DataSeries();

            killSeries.LegendText = "Kills";
            deathSeries.LegendText = "Deaths";
            quitSeries.LegendText = "Leavers";

            DataPoint dataPoint = null;

            quitSeries.RenderAs = RenderAs.Point;

            foreach (Player p in selectedGame.PlayerList)
            {
                if (p != null && p.Quit)
                {
                    dataPoint = new DataPoint();
                    TimeSpan leaveTime = p.QuitAt;
                    float leaveTimePercent = selectedGame.GetTimePercent(p.QuitAtString);
                    dataPoint.XValue = (int)leaveTimePercent;
                    dataPoint.YValue = selectedPlayer.KillCount / 2;
                    dataPoint.AxisXLabel = String.Format("{1} left at {0}", leaveTime.ToString(), p.Name);
                    quitSeries.DataPoints.Add(dataPoint);
                }
            }

            chart.Series.Add(quitSeries);

            killSeries.RenderAs = RenderAs.Line;
            deathSeries.RenderAs = RenderAs.Line;

            dataPoint = new DataPoint();
            dataPoint.YValue = 0;

            killSeries.DataPoints.Add(dataPoint);
            deathSeries.DataPoints.Add(dataPoint);

            int killCount = 1;
            int deathCount = 1;

            foreach (Kill kill in selectedPlayer.kills)
            {
                if (kill.Type != Kill.KillType.Hero)
                    continue;

                dataPoint = new DataPoint();
                dataPoint.YValue = killCount;

                float killTimePercent = selectedGame.GetTimePercent(kill.TimeString);

                TimeSpan killTime = kill.Time;

                dataPoint.XValue = (int)killTimePercent;
                dataPoint.AxisXLabel = String.Format("Killed {0} at {1}", kill.VictimName, killTime.ToString());

                killSeries.DataPoints.Add(dataPoint);
                killCount++;
            }

            foreach (Kill death in selectedPlayer.deaths)
            {
                dataPoint = new DataPoint();
                dataPoint.YValue = deathCount;

                float deathTimePercent = selectedGame.GetTimePercent(death.TimeString);

                TimeSpan deathTime = death.Time;

                dataPoint.XValue = (int)deathTimePercent;
                dataPoint.AxisXLabel = String.Format("Killed by {0} at {1}", death.KillerName, deathTime.ToString());

                deathSeries.DataPoints.Add(dataPoint);
                deathCount++;
            }

            killsSeries.Add(killSeries);
            deathsSeries.Add(deathSeries);

            chart.Series.Add(killSeries);
            chart.Series.Add(deathSeries);
            heroKillGrid.Children.Add(chart);
        }

        void dataPoint_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void deathCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            foreach (DataSeries s in deathsSeries)
                chart.Series.Add(s);
        }

        private void deathCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            foreach (DataSeries s in deathsSeries)
                chart.Series.Remove(s);
        }

        private void killCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            foreach (DataSeries s in killsSeries)
                chart.Series.Add(s);
        }

        private void killCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            foreach (DataSeries s in killsSeries)
                chart.Series.Remove(s);
        }
    }
}
